 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pro_calon_penyewa extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('pro_calon_penyewa_model', 'Tenant');
		$this->secure_session();
		
	}
	
	function index(){
		$data['title'] = "Calon Penyewa";
		viewByTemplate('pro_calon_penyewa/index', $data, 'grid');
	}
	

	function create()	
	{
		// Display Form Create
		$data['title'] = "Input Data Calon Penyewa";
		$data['data'] = NULL;
		//$data['arr_reference']	= $this->Combo->SampleReference();
		$data['mode'] = "create";
		$query = $this->db->query("SELECT no_tenant FROM (SELECT * FROM pro_tenant ORDER BY no_tenant DESC) WHERE rownum <=1");
				 				if ($query->row_array()) {
				 					$getquery = $query->row_array();
				 					$rowcount = $getquery['no_tenant'];
				 					$autonum = $rowcount + 1;
				 				} else {
								$autonum = 1;
							}
		$data['kode_tenant_auto'] = $autonum;
		viewByTemplate('pro_calon_penyewa/form', $data, 'form');
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Ubah Data Calon Penyewa";
		$data['data'] = $this->Tenant->GetSingleView(get('no_tenant'));
		$data['id_lokasi_edit'] = get('id_lokasi');
		$data['id_sewa_edit'] = get('id_sewa');
		//var_dump($data['id_lokasi']);
		$data['mode'] = "edit";
		viewByTemplate('pro_calon_penyewa/form', $data, 'form');
	}
	
	function view()
	{
		// Display Form View
		$data['title'] = "View Data Tenant";
		$data['data'] = $this->Tenant->GetSingleView(get('no_tenant'));
		
		$data['mode'] = "view";
		viewByTemplate('pro_calon_penyewa/form', $data, 'form');
	}


	function getdata()
	{
		$this->secure_session();
		echo $this->Tenant->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");
		// Validation
		
		$check = $this->db->query("SELECT COUNT(1) AS check_kode FROM pro_tenant WHERE no_kontrak= '".$this->input->post("no_kontrak")."'");
		$result = $check->row_array();
		//var_dump($result['check_kode'],$this->input->post("no_kontrak"));

		if (post("no_tenant") == '') {
			$error->Add("no_tenant", "No Tenant", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT COUNT(1) FROM pro_tenant WHERE no_tenant= '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("no"))))."'")) > 0) {
			$error->Add("no_tenant", "No Tenant", $tpl_duplicate);
		} else if (post("nama_tenant") == '') {
			$error->Add("nama_tenant", "Nama Tenant", $tpl_empty);
		} else if (post("mode")=="create" && post("no_kontrak")!=null){
			$check1 = $this->db->query("SELECT COUNT(1) AS check_kode FROM pro_tenant WHERE no_kontrak= '".$this->input->post("no_kontrak")."'");
			$result1 = $check1->row_array();
			if (post("mode") == "create" && $result1['check_kode']!='0') {
				$error->Add("no_kontrak", "No Kontrak", $tpl_duplicate);
			}
		} else if (post("no_toko") == '') {
			$error->Add("no_toko", "No Toko", $tpl_empty);
		} else if (post("jenis_usaha") == '') {
			$error->Add("jenis_usaha", "Jenis Usaha", $tpl_empty);
		} else if (post("lokasi_tempat") == '') {
			$error->Add("lokasi_tempat", "Lokasi Tempat", $tpl_empty);
		} else if (post("luas_tempat") == '') {
			$error->Add("luas_tempat", "Luas Tempat", $tpl_empty);
		} else if (post("alamat_lengkap") == '') {
			$error->Add("alamat_lengkap", "Alamat Lengkap", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				
			/*	if (post("mode") == "create")
				{
					$lokasi_tempat = $this->input->post('lokasi_tempat');
				}
				else if (post("mode") == "edit")
				{
					$lokasi_tempat = $this->input->post('lokasi_tempat2');
				}*/

				$data	=  array(
					'no_tenant' => $this->input->post('no_tenant'),
					'nama_tenant' => $this->input->post('nama_tenant'),
					'no_toko' => $this->input->post('no_toko'),
					'jenis_usaha' => $this->input->post('jenis_usaha'),
					'lokasi_tempat' => $this->input->post('lokasi_tempat'),
					'luas_tempat' => $this->input->post('luas_tempat'),
					'alamat_lengkap' => $this->input->post('alamat_lengkap'),
					'status_anggota' => $this->input->post('status_anggota'),
					'no_anggota' => $this->input->post('no_anggota'),
					'no_id_card' => $this->input->post('no_id_card'),
					'no_telephone' => $this->input->post('no_telephone'),
					'no_handphone' => $this->input->post('no_handphone'),
					'status_tenant' => $this->input->post('status_tenant'),
					'no_kontrak' => $this->input->post('no_kontrak'),
					'periode_awal_sewa' => $this->input->post('periode_awal_sewa'),
					'periode_akhir_sewa' => $this->input->post('periode_akhir_sewa'),
					'jangka_waktu' => $this->input->post('jangka_waktu'),
					'harga_sewa' => $this->input->post('harga_sewa'),
					'keterangan' => $this->input->post('keterangan'),
					'id_sewa' => $this->input->post('id_sewa'),
					'id_lokasi' => $this->input->post('id_lokasi'),
					);

 					 $data['periode_awal_sewa']			= ($data['periode_awal_sewa'] == '' ? NULL : date_format3($data['periode_awal_sewa'], 'YYYY-MM-DD'));
			   		 $data['periode_akhir_sewa']		= ($data['periode_akhir_sewa'] == '' ? NULL : date_format3($data['periode_akhir_sewa'], 'YYYY-MM-DD'));


				if ($this->input->post('status_tenant') == "Tenant"){
					$dataStatusSewa = array('status_sewa' => 'Sudah Di Sewa');
					$this->Tenant->UpdateStatusSewa($dataStatusSewa, array('id_sewa' => $this->input->post('id_sewa')));
				}

				if (post("mode") == "create")
				{

					$this->Tenant->Add($data);

					$ls_response = "Data Tersimpan";

				}
				else if (post("mode") == "edit")
				{
					
					$this->Tenant->Update($data, array('no_tenant'	=> $data['no_tenant']));

					$ls_response = "Data Tersimpan [edited]";
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$no_tenant	= get('no_tenant');
		$pesan = "Data Terhapus";
		if ($no_tenant != "")
		{
			// Delete Single
			$arr_rule	= array('no_tenant' => $no_tenant);
			$ls_result = $this->Tenant->DeleteSingle($arr_rule);

		}
		else
		{
		}
		
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row = $this->data['deleted'][$i];
				
				$arr_rule	= array('no_tenant' => $row['no_tenant']);
				$ls_result .= $this->Tenant->DeleteSingle($arr_rule);
			}
		echo $ls_result;
		echo $pesan;
	}
}
?>